﻿using UnityEngine;
using System.Collections;

public class EPCheckBox : ObjectBase
{
    Transform m_On;
    Transform m_Off;
    Transform m_Disable;

    public GameObject EventRecieaver;
    public string FuncName = "OnCheckBoxButton";
    public string SoundNameOn = "UI_check_box";
    public string SoundNameOff = "UI_check_box";
   
    public bool IsChecked
    {
        get { return m_IsChecked; }
        set 
        { 
            m_IsChecked = value;
            SetState();
        }
    }
    bool m_IsChecked = false;   

    public bool isEnabled 
    { 
        get
        {
            return enabled; 
        } 
        set
        {
            enabled = value;
            SetDisable();
        } 
    }

	void Awake()
    {
        m_On = transform.Find("on");
        m_Off = transform.Find("off");       
        m_Disable = transform.Find("disable");

        SetState();
    }

    void OnPress(bool isPressed)
    {
        if (isEnabled && UICamera.currentTouch != null)
        {           
            if (isPressed)
            {
                
            }
            else if (UICamera.currentTouch.current == gameObject)
            {
                IsChecked = !IsChecked;
               
                if (IsChecked)
                    SoundController.PlaySound(SoundNameOn);
                else
                    SoundController.PlaySound(SoundNameOff);               
            }            
        }        
    }

    void SetDisable()
    {
        if (m_Disable != null)
            m_Disable.gameObject.SetActive(!isEnabled);
        if (m_On != null)
            m_On.gameObject.SetActive(isEnabled);
        if (m_Off != null)
            m_Off.gameObject.SetActive(isEnabled);     
    }

	void SetState()
    {
        m_Off.gameObject.SetActive(!IsChecked);
        m_On.gameObject.SetActive(IsChecked);

        if (EventRecieaver != null)
            EventRecieaver.SendMessage(FuncName, this);
    }
}
